package com.mybatis.boost.generator.xml;

import org.mybatis.generator.api.dom.DefaultXmlFormatter;
import org.mybatis.generator.api.dom.OutputUtilities;
import org.mybatis.generator.api.dom.xml.Document;

/**
 * @author jearton
 * @since 2016/11/22
 */
public class CustomXmlFormatter extends DefaultXmlFormatter {

    @Override
    public String getFormattedContent(Document document) {
        XmlElement rootElement = new XmlElement(document.getRootElement());
        document.setRootElement(rootElement);
        StringBuilder sb = new StringBuilder();

        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$

        if (document.getPublicId() != null && document.getSystemId() != null) {
            OutputUtilities.newLine(sb);
            sb.append("<!DOCTYPE "); //$NON-NLS-1$
            sb.append(rootElement.getName());
            sb.append(" PUBLIC \""); //$NON-NLS-1$
            sb.append(document.getPublicId());
            sb.append("\" \""); //$NON-NLS-1$
            sb.append(document.getSystemId());
            sb.append("\">"); //$NON-NLS-1$
        }

        OutputUtilities.newLine(sb);
        sb.append(rootElement.getFormattedContent(0));
        return sb.toString();
    }
}
